package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.SystemArticle;
import com.nondo.dean.platform.service.SystemArticleService;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/25
 */
@RestController("apiAdminPlatformSystemArticle")
@RequestMapping("/{v}/admin/system/article")
public class SystemArticleController {
    @Autowired
    SystemArticleService systemArticleService;

    /**
     * 查看文章详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<SystemArticle> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<SystemArticle> view = new View<SystemArticle>(20000, "获取数据成功");
        
        SystemArticle article = null;
        if (id != null) {
            article = systemArticleService.select(id);
        } else {
            article = new SystemArticle();
        }

        view.setData(article);
        return view;
    }
    
    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<SystemArticle>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<SystemArticle> query) {
        View<Page<SystemArticle>> view = new View<Page<SystemArticle>>(20000, "获取数据成功");

        SystemArticle systemArticle = query.getData();

        Page<SystemArticle> page = new Page<SystemArticle>(query.getPage(), query.getLimit(), systemArticleService.count(systemArticle));

        List articleList = systemArticleService.getList(query.getOffset(), query.getLimit(), systemArticle);

        page.setContent(articleList);

        view.setData(page);

        return view;
    }

    /**
     * 添加
     *
     * @param request
     * @param response
     * @param systemArticle
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<SystemArticle> add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SystemArticle systemArticle, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.systemArticleService.add(systemArticle);
        }

        return view;
    }

    /**
     * 更新
     *
     * @param request
     * @param response
     * @param systemArticle
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<SystemArticle> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SystemArticle systemArticle, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.systemArticleService.update(systemArticle);
        }

        return view;
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param articleId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/delete/{articleId}")
    public View<SystemArticle> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable int articleId) {
        View view = new View(20000, "删除成功");

        SystemArticle systemArticle = this.systemArticleService.select(articleId);
        this.systemArticleService.delete(systemArticle);

        return view;
    }
}
